package jltx.fragmentpageframework.simple.fragment.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xq.recyclerviewlib.CommonQuickAdapter;

import java.util.ArrayList;

import cn.jltx.support.SupportFragment;
import cn.jltx.utils.ViewHelper;
import jltx.fragmentpageframework.R;
import jltx.fragmentpageframework.simple.adapter.MenuListAdapter;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/07.
 */

public class MenuListFragment extends SupportFragment {

    private static final String ARG_MENUS = "arg_menus";
    private static final String ARG_CURPOS = "arg_curpos";
    private static final String SAVE_STATE_POSITION = "save_state_position";
    private ArrayList<String> mMenus;
    private int mCurrentPosition = -1;

    private RecyclerView recyclerView;
    private MenuListAdapter menuListAdapter;

    public static MenuListFragment newInstance(ArrayList<String> menus){
        Bundle args = new Bundle();
        args.putStringArrayList(ARG_MENUS,menus);  //传递左边菜单
        MenuListFragment fragment = new MenuListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if(args != null){
            mMenus = args.getStringArrayList(ARG_MENUS);
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_simple_shop_menu,container,false);
        recyclerView = ViewHelper.findViewById(rootView,R.id.recv_shop_menu);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(savedInstanceState);
    }

    private void initView(Bundle savedInstanceState){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        menuListAdapter = new MenuListAdapter(mMenus);
        menuListAdapter.setOnRecyclerViewItemClickListener(itemClickListener);
        recyclerView.setAdapter(menuListAdapter);

        if(savedInstanceState != null){
            mCurrentPosition = savedInstanceState.getInt(SAVE_STATE_POSITION);
            menuListAdapter.setItemChecked(mCurrentPosition);
        }else {
            mCurrentPosition = 0;
            menuListAdapter.setItemChecked(mCurrentPosition);
        }
    }

    private CommonQuickAdapter.OnRecyclerViewItemClickListener itemClickListener = new CommonQuickAdapter.OnRecyclerViewItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            showContent(position);
            if(getParentFragment() instanceof ShopFragment){
                ((ShopFragment)getParentFragment()).switchContentfragment(ContentFragment.newInstance(mMenus.get(position)));
            }
        }
    };

    private void showContent(int position){
        if(position == mCurrentPosition){
            return;
        }
        mCurrentPosition = position;
        menuListAdapter.setItemChecked(position);
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVE_STATE_POSITION,mCurrentPosition);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        recyclerView.setAdapter(null);
    }

}
